Skip to content

Client: completed requests no longer keep their responses alive - #4870

Merged
marcschier merged 1 commit into
masterfrom
romanett/client-gc-survival-at-scale-65c667
Oct 21, 2026
Merged

marcschier merged 1 commit into
masterfrom
romanett/client-gc-survival-at-scale-65c667

Conversation

@romanett

Copy link
Copy Markdown
Contributor

Problem

A client request that lives across a GC is promoted. Publish requests always are: they wait at the server for the publishing interval. When the response arrives, two dead old objects still reference it:

  1. UaSCUaBinaryChannel.WriteOperation.MessageBody: UaSCUaBinaryClientChannel.SendRequestAsync read it but never cleared it.
  2. The box of the generated SessionClient.*Async method. It is async ValueTask<T> with the default builder, so a call that completes asynchronously leaves a Task<T> box holding the result and the hoisted response locals.

The GC treats old objects as live. Every gen0 and gen1 collection therefore keeps the whole response alive through the card table until a full collection. With 100k monitored items, every PublishResponse (100 MonitoredItemNotifications) ended up promoted to gen2.

Evidence (ClrMD scan of a live client dump, 25 sessions × 40 subscriptions × 100 items): 1,073 KB of gen0 was kept only by dead old objects, against 136 KB kept by live ones. The dead holders were exactly the two above, holding the same responses. Gen2 held 37 MB of dead notifications.

Change

Contract note: the generated SessionClient calls now return pooled value tasks, so each must be awaited once, like any value task. All sync-over-async call sites in the repo use .AsTask() first.

After the fix, young data kept only by dead old objects is 0 KB (was 1,073 KB).

Measurements

perf/ServerLoadHarness, client client-gc: line. Master vs this branch, 3 alternating rounds, runs gated on foreign CPU load (shared machine). Ranges are over the rounds.

scenario build pause share GC p50 ms survived/gen0 survived/gen1 gen2 MB B/op
sub 100k items (25×40×100, pub 500 ms) master 4.9–5.4 % 7.9–8.2 2.28–2.57 MB 1.4–1.7 MB 121–123 235–238
this PR 4.0–4.5 % 6.1–6.9 0.64–0.71 MB 0.19 MB 86 233–240
sub 50k items (25×20×100) master 1.7–2.2 % 5.5–6.5 2.1–2.2 MB 0.8–1.1 MB 56–62 233–238
this PR 1.7–1.8 % 5.2–5.5 0.39–0.41 MB 0.1–0.2 MB 44–49 232
read 1 node, 200 sessions master 8.9–9.2 % 2.2–2.4 200 KB 20 1456
this PR 7.3–7.7 % 2.2–2.7 113–163 KB 20 1272
read 1 node, 20 sessions master 3.3–3.6 % 0.9–1.2 50–53 KB 4.4 1456
this PR 2.8–2.9 % 0.85–1.16 32–46 KB 4.4 1272

The first fix round at 100k ran while another process starved the server (46k notif/s); it is left out of the fix row.

  • At 50k items, gen1 collections drop from 15 to 1 per 10 s.
  • GC phases at 100k items (TraceAnalyzer gc): card marking 3.3 → 2.55 ms (2.8 → 0.72 MB marked), plan+relocate+compact 6.05 → 3.27 ms.
  • Allocation: −184 B per request (−12.6 %), because the Task box per call is gone.

Tests

net10.0: Opc.Ua.Client.Tests 3283 passed / 0 failed, Opc.Ua.Core.Tests 6535/0, Opc.Ua.SourceGeneration.Core.Tests 4412/0, Opc.Ua.Sessions.Tests 857/0. The net48 Release build of Opc.Ua.Client (with Core) is clean.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 21, 2026 14:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused changes preserve response handling, follow existing pooling patterns, and have no unresolved findings.

No open issues 🎉

What changed in this PR

Reduces client response retention after completed requests, extending the pooled state-machine approach from #4849.

Changes:

  • Clears the channel operation’s response reference after reading it.
  • Uses pooled state machines for generated client async methods on .NET 6+, documenting single-await usage.
File Description
tools/​Opc.Ua.SourceGeneration.Core/​Generators/​ClientApiTemplates.cs Enables pooled async builders and documents task consumption.
src/​Opc.Ua.Core/​Stack/​Tcp/​UaSCBinaryClientChannel.cs Releases the completed operation’s response reference.

Review effort: Balanced

A request that waits long enough to be promoted (a Publish waits for the
publishing interval) kept its response reachable from two dead old objects:
the channel's WriteOperation (MessageBody) and the Task box of the generated
SessionClient.*Async method (result and hoisted locals). Every gen0 and gen1
collection then kept the whole response through the card table until a full
collection, so publish responses were promoted to gen2.

- UaSCUaBinaryClientChannel.SendRequestAsync clears MessageBody once read.
- Generated SessionClient async methods use PoolingAsyncValueTaskMethodBuilder
  on .NET 6+: a completed box drops its result and state machine.

100k monitored items: young data kept only by dead old objects 1073 KB -> 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Code coverage

✅ Coverage gate passed.

Check Result Threshold
✅ Project line rate 89.89% (399186/444075 lines) >= 70.00%
✅ Project branch rate 80.43% >= 60.00%
✅ Patch coverage 100.00% (14/14 changed lines) >= 60.00% (<= 100 changed lines, advisory)
ℹ️ Baseline delta (advisory) +16.29 pp 73.60% recorded

Coverage is above the recorded baseline - consider ratcheting coverage-thresholds.json.

Thresholds live in coverage-thresholds.json. Whole report before exclusions: line 89.05%, branch 79.62%.

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.31%. Comparing base (4d00467) to head (4baf2f4).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...c/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4870   +/-   ##
=======================================
  Coverage   84.31%   84.31%           
=======================================
  Files        3007     3007           
  Lines      444062   444075   +13     
  Branches    76909    76909           
=======================================
+ Hits       374399   374433   +34     
+ Misses      44688    44676   -12     
+ Partials    24975    24966    -9     
Flag Coverage Δ
actions 84.31% <92.85%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ceGeneration.Core/Generators/ClientApiTemplates.cs 100.00% <100.00%> (ø)
...c/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs 68.22% <66.66%> (+0.06%) ⬆️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marcschier
marcschier merged commit 48871a7 into master Oct 21, 2026
145 checks passed
@romanett
romanett deleted the romanett/client-gc-survival-at-scale-65c667 branch October 27, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants